<?php
/**
+--------------------------------------------------------------
 * User: maqun
+--------------------------------------------------------------
 * Date: 2015/12/11
+--------------------------------------------------------------
 * Time: 17:13
+--------------------------------------------------------------
 * 竞赛&作业列表
+--------------------------------------------------------------
 */
class ContestAction extends BaseAction{

    protected  $page_size = 20;

    public function _initialize(){
        self::$action = 5;
        parent::_initialize();
    }
    public function index(){
//        $grade = D('Users')->getGradeInfo();//获取各个年级
        $keywords   = !empty($_GET['keywords']) ? trim($_GET['keywords']) : '';
        $title_tag  = '';

        $fields = " `contest_id`,`title`,unix_timestamp(`start_time`) as start_time,unix_timestamp(`end_time`) as end_time,`description`,`private`,`langmask` ";
        $where  = " where 1  and `defunct`='N' ";
        if(!empty($keywords)){
            $where .= " and locate('{$keywords}',title) ";
        }
        vendor('Page');
        $total_sql = "select count(DISTINCT (contest_id)) as total from `contest` {$where}";
        $res = D('Contest')->query($total_sql);
        $count = !empty($res[0]['total']) ? intval($res[0]['total']):0;
        $page  = new Page($count,$this->page_size);

        $sql   = "select {$fields} from `contest` {$where} order by `contest_id` DESC limit ".$page->firstRow.",".$page->listRows;
        $list  = D('Contest')->query($sql);

        $this->assign('keywords',$keywords);
        $this->assign('now',time());//当前时间
        $this->assign('list',$list);
        $this->assign('page',$page->show());
        $this->display('index');
    }

    /**
     *查看竞赛
     */
    public function view(){

        $contest_id = !empty($_GET['id']) ? intval($_GET['id']) :$this->error('访问的数据不存在');
        $fields = " `contest_id`,`title`,unix_timestamp(`start_time`) as start_time,unix_timestamp(`end_time`) as end_time,`description`,`private`,`langmask` ";
        $contest_info = D('Contest')->field($fields)->where('contest_id='.$contest_id)->limit(1)->select();
        if(empty($contest_info)){
            $this->error('访问的数据不存在');
        }
        $table1 = " (select count(problem_id) as solved,problem_id from solution where contest_id='{$contest_id}' and result=4 group by problem_id) ";
        $table2 = " (select count(problem_id) as submit,problem_id from solution where contest_id='{$contest_id}' group by problem_id) ";

        $problem_id = "select problem_id from contest_problem where contest_id='{$contest_id}' ";
        $table  = " problem as a left join {$table1} as b on a.problem_id=b.problem_id left join {$table2} as c on a.problem_id=c.problem_id ";
        $fields = " a.title,a.problem_id,a.source,b.solved,c.submit ";
        $total_sql = " select count(*) as total from {$table} where a.problem_id in($problem_id) and a.defunct='N'";

        vendor('Page');
        $res = D('Problem')->query($total_sql);
        $count = !empty($res[0]['total']) ? intval($res[0]['total']):0;
        $page  = new Page($count,$this->page_size);

        $sql = " select {$fields} from {$table} where a.problem_id in($problem_id) and a.defunct='N' limit ".$page->firstRow.",".$page->listRows;

        $list= D('Problem')->query($sql);

        $this->assign('list',$list);
        $this->assign('now',time());
        $this->assign('contest_info',$contest_info[0]);
        $this->display('view');

    }
    //竞赛排名
    public function rank(){


    }
}